<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <!-- 注册表单 -->
      <el-form ref="form" size="large" autocomplete="off" v-if="isRegister" :model="registerData" :rules="registerRules">
        <el-form-item>
          <h1>管理员注册</h1>
        </el-form-item>
        <el-form-item prop="adminName">
          <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="registerData.adminName"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="registerData.password"></el-input>
        </el-form-item>
        <el-form-item prop="rePassword">
          <el-input :prefix-icon="Lock" type="password" placeholder="请再次输入密码" v-model="registerData.rePassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="button" type="primary" @click="register">注册</el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="toggleRegister">← 返回登录</el-link>
        </el-form-item>
      </el-form>
      <!-- 登录表单 -->
      <el-form ref="form" size="large" autocomplete="off" v-else :model="loginData" :rules="loginRules">
        <el-form-item>
          <h1>管理员登录</h1>
        </el-form-item>
        <el-form-item prop="adminName">
          <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="loginData.adminName"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="loginData.password"></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button class="button" type="primary" @click="login">登录</el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="toggleRegister">注册 →</el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { adminRegister, adminLogin } from '@/api/admin.js'
import { useTokenStore } from '@/stores/token.js'
import { useRouter } from 'vue-router'

const isRegister = ref(false)

const loginData = ref({
  adminName: '',
  password: ''
})

const registerData = ref({
  adminName: '',
  password: '',
  rePassword: ''
})

const loginRules = {
  adminName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
  ]
}

const registerRules = {
  adminName: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
  ],
  rePassword: [
    { validator: (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次确认密码'))
        } else if (value !== registerData.value.password) {
          callback(new Error('请确保两次输入的密码一样'))
        } else {
          callback()
        }
      }, trigger: 'blur'
    }
  ]
}

const tokenStore = useTokenStore()
const router = useRouter()

const toggleRegister = () => {
  isRegister.value = !isRegister.value
  clearFormData()
}

const clearFormData = () => {
  loginData.value = { adminName: '', password: '' }
  registerData.value = { adminName: '', password: '', rePassword: '' }
}

const login = async () => {
  let result = await adminLogin(loginData.value)
  if (result.code === 0) {
    ElMessage.success(result.message ? result.message : '登录成功')
    tokenStore.setToken(result.data)
    await router.push('/')
  } else {
    ElMessage.error(result.message ? result.message : '登录失败')
  }
}

const register = async () => {
  let result = await adminRegister(registerData.value)
  if (result.code === 0) {
    ElMessage.success(result.message ? result.message : '注册成功')
    toggleRegister()
  } else {
    ElMessage.error(result.message ? result.message : '注册失败')
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;

  .bg {
    background:
    url('@/assets/login_bg.jpg') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }

  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;

    .title {
      margin: 0 auto;
    }

    .button {
      width: 100%;
    }

    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
